package com.yzmoney.common.constant;

import com.yzmoney.common.property.PropertyReader;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Objects;

/**
 * Created by LiuJun on 2015/7/17.
 */
public class PropertyUtils {
    private static PropertyReader reader = PropertyReader.getInstance(Constants.COMMON_PATH);

    public static int getValue(String key, int defaultValue) {
        int value = NumberUtils.toInt(Objects.toString(reader.getText(key)), defaultValue);
        return value;
    }

    public static String getValue(String key, String defaultValue) {
        String value = Objects.toString(reader.getText(key), defaultValue);
        value = StringUtils.trim(value);
        return value;
    }
    /**得到MSG*/
    public static String getMsg(String key, Object... args) {
        return reader.getMessage(key, args);
    }
    /**是否包含key*/
    public static boolean contains(String key) {
        return reader.containsKey(key);
    }

    public static String getCookieId() {
        return getValue(Constants.PROPERTY_KEY_COOKIE_ID, "cookieId");
    }

    /**得到资源根目录*/
    public static String getResourceRootPath (){
        return getValue(Constants.PROPERTY_KEY_RES_PATH, "/");
    }
    /**得到资源访问起始地址*/
    public static String getResourceRootUrl (){
        return getValue(Constants.PROPERTY_KEY_RES_URL, "/");
    }

    public static int getValue(String key, int defaultValue,String filePath) {
        PropertyReader fileReader = StringUtils.isEmpty(filePath)?reader:PropertyReader.getInstance(filePath);
        int value = NumberUtils.toInt(Objects.toString(fileReader.getText(key)), defaultValue);
        return value;
    }

    public static String getValue(String key, String defaultValue,String filePath) {
        PropertyReader fileReader = StringUtils.isEmpty(filePath)?reader:PropertyReader.getInstance(filePath);
        String value = Objects.toString(fileReader.getText(key), defaultValue);
        value = StringUtils.trim(value);
        return value;
    }
}
